package com.elepro.shoppingmall.service.goods;


import com.elepro.shoppingmall.entity.Goods;
import com.elepro.shoppingmall.entity.GoodsType;
import com.elepro.shoppingmall.mapper.goods.GoodsMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GoodsServiceIm implements GoodsService{

    @Resource
    GoodsMapper goodsMapper;
    //Goods
    //读取所有产品
    //@Cacheable(cacheNames = "food", unless = "#result == null")
    public List<Goods> sLoadAllGoods() {
        return goodsMapper.loadAllGoods();
    }

    //分页读取所有产品
    public PageInfo<Goods> pageGoodsList(int pageNumber, int size) {
        PageHelper.startPage(pageNumber, size);
        List<Goods> goodsList = goodsMapper.loadAllGoods();
        return new PageInfo<>(goodsList);
    }

    //根据GoodsTypeId查询所有产品
    //@Cacheable(cacheNames = "food", unless = "#result == null")
    public List<Goods> sLoadGoodsById(long id) {
        return goodsMapper.loadGoodsById(id);
    }


    //GoodsType

    //查询所有产品类型
    // @Cacheable(cacheNames = "foodType", unless = "#result == null")
    public List<GoodsType> sLoadAllGoodsType() {
        return goodsMapper.loadAllGoodsType();
    }

    //查找产品类型Id
    public int sFindGoodsTypeId(String goodsType) {
        try {
            return goodsMapper.findGoodsTypeId(goodsType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //增加产品
    // @CacheEvict(cacheNames = "food", allEntries = true)
    public int sInsertGoods(Goods goods) {
        try {
            return goodsMapper.insertGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //增加商品类型
    public int sInsertGoodsType(GoodsType goodsType) {
        try {
            return goodsMapper.insertGoodsType(goodsType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //通过Id删除产品
    // @CacheEvict(cacheNames = "food", allEntries = true)
    public int sDeleteGoodsById(long id) {
        try {
            return goodsMapper.deleteGoodsById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //删除产品类型
    public int sDeleteGoodsType(GoodsType goodsType) {
        try {
            return goodsMapper.deleteGoodsType(goodsType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //通过goodsTypeId删除所有产品
    // @CacheEvict(cacheNames = "food", allEntries = true)
    public int sDeleteAllGoodsByGoodsTypeId(long goodsTypeId) {
        try {
            return goodsMapper.deleteAllGoodsByGoodsTypeId(goodsTypeId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //修改产品
    //@CacheEvict(cacheNames = "food", allEntries = true)
    public int sUpdateGoods(Goods goods) {
        try {
            return goodsMapper.updateGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //修改产品类型
    public int sUpdateGoodsType(GoodsType goodsType) {
        try {
            return goodsMapper.updateGoodsType(goodsType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    //搜索产品
    public List<Goods> sFindGoodsBySearch(String goodsName) {
        return goodsMapper.findGoodsBySearch("%" + goodsName + "%");
    }

    //查询商品总数

    @Override
    public Long countAllGoods() {
        return goodsMapper.countAllGoods();
    }
}
